package com.controller;

import com.model.Student;
import com.service.StudentService;
import com.utils.PageParam;
import com.utils.Pager;
import com.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("student")
@RestController
public class StudentController {

    @Resource
    private StudentService studentService;

    @RequestMapping(value = "/getStudentPage")
    public R getStudentPager(String clazzCode, String studentName, Integer pageIndex, Integer pageSize) {
        Pager<Student> pager = studentService.getStudentPage(clazzCode, studentName, new PageParam(pageIndex, pageSize));
        return R.ok().put("data", pager);
    }

    @RequestMapping(value = "/getClazzStudents")
    public R getClazzStudents(String clazzCode) {
        List<Student> students = studentService.getClazzStudents(clazzCode);
        return R.ok().put("data", students);
    }

    @RequestMapping("/saveStudent")
    public R saveStudent(String studentId, String chineseName, String englishName, String tel, String stuClass,
                         String className, String stuStatus){
        Student student = new Student();
        student.setStudentId(studentId);
        student.setChineseName(chineseName);
        student.setEnglishName(englishName);
        student.setTel(tel);
        student.setStuClass(stuClass);
        student.setClassName(className);
        student.setStuStatus(stuStatus);
        if (studentId != null) {
            studentService.updateStudent(student);
        } else {
            studentService.saveStudent(student);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delStudent")
    public R delChild(String ids){
        String[] strArr = ids.split(",");
        List<Long> idList = new ArrayList<>();
        for (String str : strArr) {
            idList.add(Long.valueOf(str));
        }
        studentService.delStudent(idList);
        return R.ok();
    }
}
